package com.wanban.adutils.advertisers.ironsource

import android.app.Activity
import android.util.Log
import com.ironsource.mediationsdk.IronSource
import com.unity3d.mediation.LevelPlayAdError
import com.unity3d.mediation.LevelPlayAdInfo
import com.unity3d.mediation.interstitial.LevelPlayInterstitialAd
import com.unity3d.mediation.interstitial.LevelPlayInterstitialAdListener
import com.wanban.adutils.advertisers.BaseAdLoader
import com.wanban.adutils.callback.OnLoadAdCallback
import com.wanban.adutils.db.bean.AdInfo
import com.wanban.adutils.tools.AdConstants


class ISInterstitialAdUtils : BaseAdLoader() {

    private var mInterstitialAd: LevelPlayInterstitialAd? = null

    override fun loadAdNew(activity: Activity, adInfo: AdInfo, callback: OnLoadAdCallback) {
        super.loadAdNew(activity, adInfo, callback)
        // 判断广告是否达到请求上线
        // Create the interstitial ad object
        mInterstitialAd = LevelPlayInterstitialAd(realUnitId)
        if (LevelPlayInterstitialAd.isPlacementCapped(realUnitId)) {
            Log.e(AdConstants.TAG, "ISInterstitialAdUtils...当前广告收到限制，无法使用...")
            // 直接返回奖励
            callback.onAdComplete()
            destroy()
            return
        }

        mInterstitialAd?.setListener(object : LevelPlayInterstitialAdListener {
            override fun onAdLoaded(adInfo: LevelPlayAdInfo) {
                // Ad was loaded successfully
                activity.runOnUiThread {
                    Log.e(AdConstants.TAG, "ISInterstitialAdUtils...onAdReady " + adInfo.getPlacementName())
                    setAdLoadSuccess()
                    if (!isTimeout) {
                        sdkLoadFinish()
                        callback.onAdLoaded()
                    }
                }

            }
            override fun onAdLoadFailed(error: LevelPlayAdError) {
                // Ad load failed
                activity.runOnUiThread {
                    Log.e(AdConstants.TAG, "ISInterstitialAdUtils...onAdLoadFailed " + error.getErrorMessage())
                    if (isTimeout) {
                        destroy()
                        return@runOnUiThread
                    }
                    val errorCode = error.getErrorCode()
                    analysisErrorCode(adInfo, errorCode)
                    sdkLoadFinish()
                    callback.onAdLoadFailed(errorCode)
                    destroy()
                }
            }
            override fun onAdDisplayed(adInfo: LevelPlayAdInfo) {
                // Ad was displayed and visible on screen
                activity.runOnUiThread {
                    setAdLoadShowing(true)
                    callback.onAdOpened()
                }
            }
            override fun onAdDisplayFailed(error: LevelPlayAdError, adInfo: LevelPlayAdInfo) {
                // Ad fails to be displayed
                // Optional
                activity.runOnUiThread {
                    setAdLoadShowing(false)
                    val code = error.getErrorCode()
                    callback.onAdLoadFailed(code)
                    destroy()
                }
            }
            override fun onAdClicked(adInfo: LevelPlayAdInfo) {
                // Ad was clicked
                // Optional
                Log.e("IS回调", "插屏 -> onAdClicked..." + adInfo.getAdUnitId())
                activity.runOnUiThread {
                    callback.onAdClicked()
                }
            }
            override fun onAdClosed(adInfo: LevelPlayAdInfo) {
                // Ad was closed
                // Optional
                Log.e("IS回调", "插屏 -> onAdClosed..." + adInfo.getPlacementName())
                activity.runOnUiThread {
                    callback.onAdComplete()
                    destroy()
                }
            }
            override fun onAdInfoChanged(adInfo: LevelPlayAdInfo) {
                // Called after the ad info is updated. Available when another interstitial ad has loaded, and includes a higher CPM/Rate
                // Optional
            }
        })

        // Check that ad is ready and that the placement is not capped
        if (mInterstitialAd?.isAdReady() == true) {
            setAdLoadSuccess()
            sdkLoadFinish()
            callback.onAdLoaded()
            return
        }

        mInterstitialAd?.loadAd()

    }


    override fun isAdLoadSuccess(): Boolean {
        return super.isAdLoadSuccess() && mInterstitialAd?.isAdReady() == true
    }



    override fun showAd(activity: Activity) {
        super.showAd(activity)
        if (mInterstitialAd?.isAdReady() == true) {
            mInterstitialAd?.showAd(activity, realUnitId)
        } else {
            mOnLoadAdCallback?.onAdLoadFailed(AdConstants.ERROR_CODE_INTERNAL_ERROR)
            destroy()
        }
    }




    override fun destroy() {
        super.destroy()
        IronSource.removeInterstitialListener()
    }
}